//
//  NumberFommatter.m
//  SohoNaru
//
//  Created by ladaka31 ladaka on 13. 6. 21..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "NumberFommatter.h"

@implementation NumberFommatter


+(NSString *)phoneNumberFomatter:(NSString*)number{
    
    NSString *result = @"";
    
    if(number == nil || [number length] <= 0 || [number isEqualToString:@" "]){
        return result;
    }
  
    number = [[number componentsSeparatedByCharactersInSet:
                     [[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                    componentsJoinedByString:@""];
    
    if([number hasPrefix:@"82"]){
        number = [number stringByReplacingOccurrencesOfString :@"82" withString:@"0"];
    }
    
    if(number.length == 8){ // 1544-1544
        result = [NSString stringWithFormat:@"%@-%@", [number substringWithRange:NSMakeRange(0, 4)], [number substringWithRange:NSMakeRange(4, 4)]];
    }else if(number.length == 9){ // 02-xxx-xxxx
        result = [NSString stringWithFormat:@"%@-%@-%@", [number substringWithRange:NSMakeRange(0, 2)], [number substringWithRange:NSMakeRange(2, 3)],  [number substringWithRange:NSMakeRange(5, 4)]];
    }else if(number.length == 10){
        if([number hasPrefix:@"02"]){ // 02-xxxx-xxxxx
            result = [NSString stringWithFormat:@"%@-%@-%@", [number substringWithRange:NSMakeRange(0, 2)], [number substringWithRange:NSMakeRange(2, 4)],  [number substringWithRange:NSMakeRange(6, 4)]];
        }else{ // 031/010/011-xxx-xxxx
            result = [NSString stringWithFormat:@"%@-%@-%@", [number substringWithRange:NSMakeRange(0, 3)], [number substringWithRange:NSMakeRange(3, 3)],  [number substringWithRange:NSMakeRange(6, 4)]];
        }
    }else if(number.length == 11){// 031/010/011-xxxx-xxxx
        result = [NSString stringWithFormat:@"%@-%@-%@", [number substringWithRange:NSMakeRange(0, 3)], [number substringWithRange:NSMakeRange(3, 4)], [number substringWithRange:NSMakeRange(7, 4)]];
    }else{
        result = number;
    }
    
    return result;
}


+(NSString *)phoneNumberSeparation:(NSString*)number{
    NSString *result;
    
    if([number rangeOfString:@"-"].location != NSNotFound)
    {
        result = [number substringToIndex:[number rangeOfString:@"-"].location];
        
    }else{
        result = number;
    }
    
    return result;
}
@end
